

#####################################
# PREPARE variables:
#
# NOTE that "app"/Contents specify slash. 
# this is not a problem even if app itself have slash at end.
# We will get something as  gg.app//Contents and this still works fine.
#
app="$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/"
app_exe="$app"/Contents/MacOS/$PRODUCT_NAME

app_vcomp="$app"/Contents/vcomponents
app_frmw="$app"/Contents/Frameworks


#####################################
# COPY files of vcomponents into APP.
#####################################
if [ ! -d "$app_vcomp" ] ; then
	mkdir "$app_vcomp"
fi

# VSHARED
if [ ! -e "$app_vcomp"/libvshared_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvshared_fat_release.dylib "$app_vcomp"
fi

# VKERNEL
if [ ! -e "$app_vcomp"/libvkernel_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvkernel_fat_release.dylib "$app_vcomp"
fi

# VCLIENT
if [ ! -e "$app_vcomp"/libvclient_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvclient_fat_release.dylib "$app_vcomp"
fi

# VREPORT
if [ ! -e "$app_vcomp"/libvreport_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvreport_fat_release.dylib "$app_vcomp"
fi


####################################
# Copy Folders:
####################################

# vresources
cp -R /usr/local/lib/vcomponents/vresources "$app_vcomp"



#####################################
# COPY /Library/Frameworks/V4CC.framework into app/Frameworks
#####################################
if [ ! -d "$app_frmw" ] ; then
	mkdir "$app_frmw"
fi

cp -R /Library/Frameworks/V4CC.framework "$app_frmw"

#
# REMOVE interface builder plugin from final .app
#
rm -d -f -r "$app_frmw"/V4CC.framework/Resources/V4CC.ibplugin


#####################################
# CHANGE executable_path for VCOMPONENTS dylibs:
#####################################

# change id of VSHARED
	install_name_tool -id "@executable_path/../vcomponents/libvshared_fat_release.dylib"                                                                    "$app_vcomp"/libvshared_fat_release.dylib

# change id of VKERNEL
	install_name_tool -id "@executable_path/../vcomponents/libvkernel_fat_release.dylib"                                                                    "$app_vcomp"/libvkernel_fat_release.dylib

	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib"   "@executable_path/../vcomponents/libvshared_fat_release.dylib"    "$app_vcomp"/libvkernel_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/VSDK.framework/Versions/A/VSDK" "@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK"  "$app_vcomp"/libvkernel_fat_release.dylib

# change id of VCLIENT
	install_name_tool -id "@executable_path/../vcomponents/libvclient_fat_release.dylib"                                                                    "$app_vcomp"/libvclient_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" "@executable_path/../vcomponents/libvshared_fat_release.dylib"      "$app_vcomp"/libvclient_fat_release.dylib

# change id of VREPORT
	install_name_tool -id "@executable_path/../vcomponents/libvreport_fat_release.dylib"                                                                    "$app_vcomp"/libvreport_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" "@executable_path/../vcomponents/libvshared_fat_release.dylib"      "$app_vcomp"/libvreport_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvkernel_fat_release.dylib" "@executable_path/../vcomponents/libvkernel_fat_release.dylib"      "$app_vcomp"/libvreport_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvclient_fat_release.dylib" "@executable_path/../vcomponents/libvclient_fat_release.dylib"      "$app_vcomp"/libvreport_fat_release.dylib



#####################################
# CHANGE executable_path for V4CC.framework dylibs:
#####################################
app_frmw_v4cc="$app_frmw"/V4CC.framework/Versions/A/V4CC
 
 	install_name_tool -id "@executable_path/../Frameworks/V4CC.framework/Version/A/V4CC"                                                                     "$app_frmw_v4cc"
  
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib"   "@executable_path/../vcomponents/libvshared_fat_release.dylib"     "$app_frmw_v4cc"
	install_name_tool -change "/usr/local/lib/vcomponents/libvkernel_fat_release.dylib"   "@executable_path/../vcomponents/libvkernel_fat_release.dylib"     "$app_frmw_v4cc"
	install_name_tool -change "/usr/local/lib/vcomponents/libvclient_fat_release.dylib"   "@executable_path/../vcomponents/libvclient_fat_release.dylib"     "$app_frmw_v4cc"
	install_name_tool -change "/usr/local/lib/vcomponents/libvreport_fat_release.dylib"   "@executable_path/../vcomponents/libvreport_fat_release.dylib"     "$app_frmw_v4cc"
    

#####################################
# now we need to update yet AppName.app/Contents/MacOS/AppName executable:
#####################################

	install_name_tool -change "/Library/Frameworks/V4CC.framework/Versions/A/V4CC"   "@executable_path/../Frameworks/V4CC.framework/Versions/A/V4CC"     "$app_exe"
